//
//  HealthCustomTypeViewController.m
//  JiuLe
//
//  Created by xcq on 15/11/9.
//  Copyright © 2015年 Fine. All rights reserved.
//
#import "Common.h"
#import "SocketManager.h"
#import "XyUpperLimitSettingViewController.h"
#import "HealthCustomTypeViewController.h"
#define getDefatulData(value1,value2) [[[defaultData objectForKey:value1] objectAtIndex:value2] floatValue];

#define getSetData(value1,value2) [[[setData objectForKey:value1] objectAtIndex:value2] floatValue];

#define KfactorSetData(value,value1) [[setData objectForKey:value] count] == 0 || [[setData objectForKey:value] objectAtIndex:value1] == nil || [[[setData objectForKey:value] objectAtIndex:value1] isEqualToString:@""]
@interface HealthCustomTypeViewController () {
    
    
}

@property (strong, nonatomic) NSNumber *xy;
@property (strong, nonatomic) NSNumber *gxy;
@property (strong, nonatomic) NSNumber *dxl;

@end

@implementation HealthCustomTypeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self showNavBarWithRightTitle:nil
                    andRightAction:nil
                     andLeftAction:@selector(backTo)
                 andRightBtnIsShow:NO];
    
    [self setUpBtn];
    [self setUpUpperLimit:NO];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

- (void)setUpUpperLimit:(BOOL)isDefault {
        CGFloat gxyLimit = 0.f;
        CGFloat dxlLimit = 0.f;
        CGFloat xyLimit = 0.f;
        
    if ((_model == nil || _model.type == nil || _model.userid == nil) || isDefault) {
        NSMutableDictionary *data = [Common loadPlist:@"HealthThresholds"];
        NSDictionary *setData = [data objectForKey:@"set"];
        NSDictionary *defaultData = [data objectForKey:@"default"];
        if (_type == HealthTypeFC) {
            if (isDefault) {
                xyLimit = getDefatulData(@"fc", 2);
                gxyLimit = getDefatulData(@"fc", 1);
                dxlLimit = getDefatulData(@"fc", 0);
            } else {
                //低心率
                if (KfactorSetData(@"fc",0)) {
                    dxlLimit = getDefatulData(@"fc", 0);
                } else {
                    dxlLimit = getSetData(@"fc", 0);
                }
                //高血压
                if (KfactorSetData(@"fc",1)) {
                    gxyLimit = getDefatulData(@"fc", 1);
                } else {
                    gxyLimit = getSetData(@"fc", 1);
                }
                if (KfactorSetData(@"fc",2)) {
                    xyLimit = getDefatulData(@"fc", 2);
                } else {
                    xyLimit = getSetData(@"fc", 2);
                }
            }
        } else if (_type == HealthTypeHZ) {
            if (isDefault) {
                xyLimit = getDefatulData(@"hz", 2);
                gxyLimit = getDefatulData(@"hz", 1);
                dxlLimit = getDefatulData(@"hz", 0);
            } else {
                //低心率
                if (KfactorSetData(@"hz",0)) {
                    dxlLimit = getDefatulData(@"hz", 0);
                } else {
                    dxlLimit = getSetData(@"hz", 0);
                }
                //高血压
                if (KfactorSetData(@"hz",1)) {
                    gxyLimit = getDefatulData(@"hz", 1);
                } else {
                    gxyLimit = getSetData(@"hz", 1);
                }
                if (KfactorSetData(@"hz",2)) {
                    xyLimit = getDefatulData(@"hz", 2);
                } else {
                    xyLimit = getSetData(@"hz", 2);
                }
            }
        } else if (_type == HealthTypeYJZ) {
            //低心率
            if (isDefault) {
                xyLimit = getDefatulData(@"yjk", 2);
                gxyLimit = getDefatulData(@"yjk", 1);
                dxlLimit = getDefatulData(@"yjk", 0);
            } else {
                if (KfactorSetData(@"yjk",0)) {
                    dxlLimit = getDefatulData(@"yjk", 0);
                } else {
                    dxlLimit = getSetData(@"yjk", 0);
                }
                //高血压
                if (KfactorSetData(@"yjk",1)) {
                    gxyLimit = getDefatulData(@"yjk", 1);
                } else {
                    gxyLimit = getSetData(@"yjk", 1);
                }
                if (KfactorSetData(@"yjk",2)) {
                    xyLimit = getDefatulData(@"yjk", 2);
                } else {
                    xyLimit = getSetData(@"yjk", 2);
                }
            }
        } else {
            if (isDefault) {
                xyLimit = getDefatulData(@"zdy", 2);
                gxyLimit = getDefatulData(@"zdy", 1);
                dxlLimit = getDefatulData(@"zdy", 0);
            } else {
                if (KfactorSetData(@"zdy", 0)) {
                    dxlLimit = getDefatulData(@"zdy", 0);
                } else {
                    dxlLimit = getSetData(@"zdy", 1);
                }
                if (KfactorSetData(@"zdy", 1)) {
                    gxyLimit = getDefatulData(@"zdy", 1);
                } else {
                    gxyLimit = getSetData(@"zdy", 1);
                }
                if (KfactorSetData(@"zdy", 2)) {
                    xyLimit = getDefatulData(@"zdy", 2);
                } else {
                    xyLimit = getSetData(@"zdy", 2);
                }
            }
        }

    } else {
        xyLimit = [_model.spo2 floatValue];
        gxyLimit = [_model.high_hr floatValue];
        dxlLimit = [_model.low_hr floatValue];
    }
    NSString *content;
    if (xyLimit != 0.f) {
      content = [NSString stringWithFormat:@"%.0f",xyLimit];
        _xyUpperLimit.attributedText = [self returnAttributedStr:content andType:3];
        _xy = @(xyLimit);
    }
    if (dxlLimit != 0.f) {
        content = [NSString stringWithFormat:@"%.0f",dxlLimit];
        _dxlUpperLimit.attributedText = [self returnAttributedStr:content andType:1];
       _dxl = @(dxlLimit);
    }
    if (gxyLimit != 0.f) {
        content = [NSString stringWithFormat:@"%.0f",gxyLimit];
        _gxyUpperLimit.attributedText = [self returnAttributedStr:content andType:2];
        _gxy = @(gxyLimit);
    }
}

- (void)refreshUpperLimit {
    [self setUpUpperLimit:NO];
   
}

- (void)setUpBtn {
    _DefaultSettingsBtn.layer.cornerRadius = 5.f;
    _saveBtn.layer.cornerRadius = 5.f;
}

- (IBAction)defaultSettingEvent:(id)sender {
//测试说不可用 回复默认值按钮
    [self setUpUpperLimit:YES];
    if ([SocketManager sharedInstance].socket.isConnected) {
        [[SocketManager sharedInstance] writeThresholdsSetDxl:[_dxl intValue]
                                                          gxy:[_gxy intValue]
                                                           xy:[_xy intValue]];
    }
}

- (IBAction)saveEvent:(id)sender {
    NSMutableDictionary *data = [Common loadPlist:@"HealthThresholds"];
    NSMutableDictionary *setData = [NSMutableDictionary dictionaryWithDictionary:[data objectForKey:@"set"]];
    NSMutableArray *temp = [NSMutableArray new];
    [temp addObject:FLOATCONVERSTR([_dxl floatValue])];
    [temp addObject:FLOATCONVERSTR([_gxy floatValue])];
    [temp addObject:FLOATCONVERSTR([_xy floatValue])];
    if (_type == HealthTypeFC) {
        [setData setObject:temp forKey:@"fc"];
    } else if (_type == HealthTypeYJZ) {
        [setData setObject:temp forKey:@"yjk"];
    } else if (_type == HealthTypeHZ) {
        [setData setObject:temp forKey:@"hz"];
    } else {
        [setData setObject:temp forKey:@"zdy"];
    }
    [data setObject:setData forKey:@"set"];

    if ([SocketManager sharedInstance].socket.isConnected) {
        [[SocketManager sharedInstance] writeThresholdsSetDxl:[_dxl intValue]
                                                          gxy:[_gxy intValue]
                                                           xy:[_xy intValue]];
        [[NSUserDefaults standardUserDefaults] setObject:self.title
                                                  forKey:UserHealthSelete];
        BOOL bol = [Common writePlist:data plistName:@"HealthThresholds"];
        if (bol) {
            JLLog(@"本地缓存成功 健康阀值");
        }
        [self backTo];
    } else {
        [SVProgressHUD showErrorWithStatus:@"当前未连接到久乐wifi，无法保存数据"];
    }
}

- (IBAction)setUpperLimit:(id)sender {
    UIButton *btn = (UIButton *)sender;
    //低心律告罄
    [self pushXyViewController:btn];
}

- (void)pushXyViewController:(UIButton *)btn {
    XyUpperLimitSettingViewController *upper = [[XyUpperLimitSettingViewController alloc] initWithNibName:@"XyUpperLimitSettingViewController" bundle:nil];
    upper.title = self.title;
    if (btn.tag == 100) {
        upper.minXy = 30;
        upper.maxXy = 60;
        upper.typeName = @"心率";
        upper.xy = [_dxl floatValue];
        upper.unit = @"次/分钟";
        upper.action = ^(CGFloat value) {
            NSString *content = [NSString stringWithFormat:@"%.0f",value];
            _dxlUpperLimit.attributedText = [self returnAttributedStr:content andType:1];
            _dxl = @(value);
        };
    } else if (btn.tag == 101) {
        upper.minXy = 100;
        upper.maxXy = 500;
        upper.typeName = @"心率";
        upper.xy = [_gxy floatValue];
        upper.unit = @"次/分钟";
        upper.action = ^(CGFloat value) {
            NSString *content = [NSString stringWithFormat:@"%.0f",value];
            _gxyUpperLimit.attributedText = [self returnAttributedStr:content andType:2];
            _gxy = @(value);
        };
    } else {
        upper.minXy = 0;
        upper.maxXy = 95;
        upper.typeName = @"血氧";
        upper.xy = [_xy floatValue];
        upper.unit = @"%";
        upper.action = ^(CGFloat value) {
            NSString *content = [NSString stringWithFormat:@"%.0f",value];
            _xyUpperLimit.attributedText = [self returnAttributedStr:content andType:3];
            _xy = @(value);
        };
    }
  
    [self.navigationController pushViewController:upper animated:YES];
}


- (NSMutableAttributedString *)returnAttributedStr:(NSString *)content andType:(int)i{
    NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:content];
 
    NSDictionary *attributeNumDic = @{NSFontAttributeName :[UIFont fontWithName:@"Helvetica" size:15.f] , NSForegroundColorAttributeName:[UIColor colorWithRed:40.f/255 green:149.f/255 blue:213.f/255 alpha:0.83]};
    NSDictionary *attributeDic = @{NSFontAttributeName :[UIFont fontWithName:@"Helvetica" size:15.f] , NSForegroundColorAttributeName:[UIColor blackColor]};
  
    [string setAttributes:attributeNumDic range:NSMakeRange(0,content.length)];
    NSString *firstStr = nil;
    NSString *lastStr = nil;
    if (i == 1 || i == 2) {
        lastStr=@"次/分钟";
    } else {
        lastStr=@"%";
    }
    if (i == 1 || i == 3) {
        firstStr = @"低于";
    } else {
        firstStr = @"高于";
    }
    NSMutableAttributedString *firstAtt = [[NSMutableAttributedString alloc] initWithString:firstStr];
    [firstAtt setAttributes:attributeDic range:NSMakeRange(0,firstStr.length)];
    NSMutableAttributedString *lastAtt = [[NSMutableAttributedString alloc] initWithString:lastStr];
    [lastAtt setAttributes:attributeDic range:NSMakeRange(0, lastStr.length)];
    [firstAtt appendAttributedString:string];
    [firstAtt appendAttributedString:lastAtt];
    return firstAtt;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
